/**
 * 医院真实数据源配置示例
 * 复制此文件为 hospitalDataSources.ts 并填入真实配置
 */

// 1. 医院信息系统(HIS) API配置
export const HIS_API_CONFIG = {
  // 生产环境
  production: {
    endpoint: 'https://his.hospital.com/api/v1',
    apiKey: 'YOUR_HIS_API_KEY',
    hospitalId: 'HOSPITAL_001',
    timeout: 10000
  },
  // 测试环境  
  staging: {
    endpoint: 'https://his-test.hospital.com/api/v1',
    apiKey: 'YOUR_TEST_API_KEY',
    hospitalId: 'TEST_HOSPITAL_001',
    timeout: 10000
  }
}

// 2. 地图服务配置
export const MAP_SERVICES_CONFIG = {
  amap: {
    key: process.env.NEXT_PUBLIC_AMAP_KEY || 'YOUR_AMAP_KEY',
    webApiKey: 'YOUR_AMAP_WEB_API_KEY', // 用于服务端调用
    searchTypes: {
      hospital: '医疗保健服务',
      pharmacy: '医疗保健服务;药店',
      clinic: '医疗保健服务;诊所'
    }
  },
  baidu: {
    key: 'YOUR_BAIDU_MAP_KEY',
    ak: 'YOUR_BAIDU_WEB_API_KEY'
  }
}

// 3. 真实医院科室数据示例
export const REAL_HOSPITAL_DEPARTMENTS = [
  {
    id: 'dept_001',
    name: '急诊科',
    floor: 'L1',
    position: [120.1551, 30.2741], // 经纬度
    phone: '0571-87065701-8801',
    director: '陈主任',
    capacity: 50,
    openTime: '24小时',
    services: ['急诊', '抢救', '观察'],
    equipment: ['心电监护仪', '除颤仪', '呼吸机'],
    address: '门诊大楼1楼东侧'
  },
  {
    id: 'dept_002', 
    name: '心内科',
    floor: 'L3',
    position: [120.1553, 30.2743],
    phone: '0571-87065701-8802',
    director: '李主任',
    capacity: 40,
    openTime: '8:00-17:30',
    services: ['心脏病诊疗', '心电图', '心脏彩超'],
    equipment: ['心电图机', '动态心电图', '心脏彩超'],
    address: '门诊大楼3楼'
  },
  // 更多科室...
]

// 4. 真实医疗设备数据示例
export const REAL_MEDICAL_EQUIPMENT = [
  {
    id: 'eq_001',
    name: 'CT-01',
    type: 'CT',
    location: '影像科',
    position: [120.1555, 30.2745],
    floor: 'L2',
    manufacturer: 'GE Healthcare',
    model: 'Revolution CT',
    status: 'active',
    lastMaintenance: '2024-01-15',
    nextMaintenance: '2024-04-15'
  },
  {
    id: 'eq_002',
    name: 'MRI-01', 
    type: 'MRI',
    location: '影像科',
    position: [120.1557, 30.2747],
    floor: 'L2',
    manufacturer: 'Siemens',
    model: 'MAGNETOM Skyra',
    status: 'active',
    lastMaintenance: '2024-01-20',
    nextMaintenance: '2024-04-20'
  }
  // 更多设备...
]

// 5. API接口定义
export interface HospitalAPI {
  // 获取科室列表
  getDepartments(): Promise<{
    success: boolean
    data: Array<{
      id: string
      name: string
      floor: string
      position: [number, number]
      phone: string
      director: string
      capacity: number
      currentPatients: number
      status: 'active' | 'inactive' | 'maintenance'
    }>
  }>

  // 获取设备列表
  getEquipment(): Promise<{
    success: boolean
    data: Array<{
      id: string
      name: string
      type: string
      location: string
      position: [number, number]
      status: 'active' | 'inactive' | 'maintenance'
      utilization: number
    }>
  }>

  // 获取实时数据
  getRealTimeData(): Promise<{
    timestamp: string
    patientFlow: number
    bedOccupancy: number
    emergencyAlerts: Array<{
      id: string
      type: string
      location: string
      severity: 'low' | 'medium' | 'high' | 'critical'
      timestamp: string
    }>
  }>
}

// 6. 数据集成服务示例
export class HospitalDataIntegration {
  private config: typeof HIS_API_CONFIG.production

  constructor(environment: 'production' | 'staging' = 'production') {
    this.config = HIS_API_CONFIG[environment]
  }

  // 从HIS系统获取科室数据
  async fetchDepartmentsFromHIS() {
    try {
      const response = await fetch(`${this.config.endpoint}/departments`, {
        headers: {
          'Authorization': `Bearer ${this.config.apiKey}`,
          'Content-Type': 'application/json'
        },
        timeout: this.config.timeout
      })

      if (!response.ok) {
        throw new Error(`HIS API Error: ${response.status}`)
      }

      const data = await response.json()
      return data.departments.map((dept: any) => ({
        id: dept.department_id,
        name: dept.department_name,
        floor: dept.floor_number,
        position: [dept.longitude, dept.latitude],
        phone: dept.phone,
        director: dept.director_name,
        capacity: dept.bed_count,
        currentPatients: dept.current_patients,
        status: dept.status
      }))
    } catch (error) {
      console.error('HIS数据获取失败:', error)
      throw error
    }
  }

  // 从地图服务获取POI数据
  async fetchPOIFromMap(hospitalName: string) {
    const mapConfig = MAP_SERVICES_CONFIG.amap
    
    try {
      const response = await fetch(
        `https://restapi.amap.com/v3/place/text?key=${mapConfig.webApiKey}&keywords=${encodeURIComponent(hospitalName)}&types=${mapConfig.searchTypes.hospital}&city=杭州&extensions=all`
      )

      const data = await response.json()
      if (data.status === '1' && data.pois) {
        return data.pois.map((poi: any) => ({
          id: `map_${poi.id}`,
          name: poi.name,
          position: poi.location.split(',').map(Number),
          address: poi.address,
          phone: poi.tel,
          type: poi.type
        }))
      }
      throw new Error('地图POI搜索失败')
    } catch (error) {
      console.error('地图POI获取失败:', error)
      throw error
    }
  }
}

// 使用示例:
/*
// 1. 初始化数据集成服务
const dataIntegration = new HospitalDataIntegration('production')

// 2. 获取真实科室数据
const departments = await dataIntegration.fetchDepartmentsFromHIS()

// 3. 获取地图POI数据  
const pois = await dataIntegration.fetchPOIFromMap('杭州市第一人民医院')

// 4. 合并数据用于地图显示
const mapData = [...departments, ...pois]
*/